#include "render.h"
#include "ui_mainwindow.h"

Render::Render()
{

}

Render::Render(Ui::MainWindow *ui, AllData *data)
{
    this->ui = ui;
    this->data = data;
    ui->widget->addGraph();
    ui->widget->addGraph();

    connect(ui->newBtn,&QPushButton::clicked,this,&Render::increaseTrend);
    connect(ui->confirmBtn,&QPushButton::clicked,this,&Render::confirmTrend);
    connect(ui->deadAndHealBtn,&QPushButton::clicked,this,&Render::deadAndHealTrend);
}

void Render::bannerRender(DiseaseData *bannerData)
{
    ui->todayDead->setText(QString("较昨日+%1").arg(bannerData->todayDead));
    ui->todayHeal->setText(QString("较昨日+%1").arg(bannerData->todayHeal));
    ui->todayConfirm->setText(QString("较昨日+%1").arg(bannerData->todayConfirm));
    ui->todayStoreConfirm->setText(QString("较昨日+%1").arg(bannerData->todayStoreConfirm));
    ui->incrNoSymptom->setText(QString("较昨日+%1").arg(bannerData->incrNoSymptom));
    ui->todayInput->setText(QString("较昨日+%1").arg(bannerData->todayInput));

    ui->totalInput->setText(QString("%1").arg(bannerData->totalInput));
    ui->totalNoSymptom->setText(QString("%1").arg(bannerData->totalNoSymptom));
    ui->totalStoreConfirm->setText(QString("%1").arg(bannerData->totalStoreConfirm));
    ui->totalConfirm->setText(QString("%1").arg(bannerData->totalConfirm));
    ui->totalDead->setText(QString("%1").arg(bannerData->totalDead));
    ui->totalHeal->setText(QString("%1").arg(bannerData->totalHeal));
}

void Render::WorldTreeRender(QVector<DiseaseData> worldData)
{
    ui->worldTreeWidget->clear();
    for (int i = 0; i < worldData.size(); i++)
    {
        QTreeWidgetItem *item = new QTreeWidgetItem(ui->worldTreeWidget);
        //qDebug() << worldData[i].areaName << worldData[i].todayConfirm << worldData[i].totalConfirm << worldData[i].totalDead << worldData[i].totalHeal;
        item->setData(0,Qt::DisplayRole,QVariant(worldData[i].areaName));
        item->setData(1,Qt::DisplayRole,QVariant(worldData[i].todayConfirm));
        item->setData(2,Qt::DisplayRole,QVariant(worldData[i].totalConfirm));
        item->setData(3,Qt::DisplayRole,QVariant(worldData[i].totalDead));
        item->setData(4,Qt::DisplayRole,QVariant(worldData[i].totalHeal));
    }
    ui->worldTreeWidget->sortByColumn(2, Qt::DescendingOrder);
}

void Render::ChinaTreeRender(QVector<DiseaseData> chinaData)
{
    for (int i = 0; i < chinaData.size(); i++)
    {
        QTreeWidgetItem *chinaAreaItem = new QTreeWidgetItem(ui->chinaTreeWidget);
        chinaAreaItem->setData(0,Qt::DisplayRole,chinaData[i].areaName);
        chinaAreaItem->setData(1,Qt::DisplayRole,chinaData[i].todayConfirm);
        chinaAreaItem->setData(2,Qt::DisplayRole,chinaData[i].totalStoreConfirm);
        chinaAreaItem->setData(3,Qt::DisplayRole,chinaData[i].totalConfirm);
        chinaAreaItem->setData(4,Qt::DisplayRole,chinaData[i].totalDead);
        chinaAreaItem->setData(5,Qt::DisplayRole,chinaData[i].totalHeal);

        if (!chinaData[i].children.isEmpty())
        {
            for (int j = 0; j < chinaData[i].children.size(); j++)
            {
                QTreeWidgetItem *cityItem = new QTreeWidgetItem(chinaAreaItem);
                cityItem->setData(0,Qt::DisplayRole,chinaData[i].children[j].areaName);
                cityItem->setData(1,Qt::DisplayRole,chinaData[i].children[j].todayConfirm);
                cityItem->setData(2,Qt::DisplayRole,chinaData[i].children[j].totalStoreConfirm);
                cityItem->setData(3,Qt::DisplayRole,chinaData[i].children[j].totalConfirm);
                cityItem->setData(4,Qt::DisplayRole,chinaData[i].children[j].totalDead);
                cityItem->setData(5,Qt::DisplayRole,chinaData[i].children[j].totalHeal);
            }
        }
        chinaAreaItem->sortChildren(2, Qt::DescendingOrder);
    }
    ui->chinaTreeWidget->sortByColumn(2, Qt::DescendingOrder);
}

void Render::plot(AllData *data)
{
    ui->widget->setLocale(QLocale(QLocale::Chinese, QLocale::China));
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    for (int i = 0; i < data->date.size(); i++)
    {
        textTicker->addTick(i, data->date[i]);
    }

    ui->widget->xAxis->setLabel("日期");
    ui->widget->yAxis->setLabel("数量");
    ui->widget->xAxis->setRange(data->dateArr[0],data->dateArr.size());
    ui->widget->xAxis->setTicker(textTicker);
    ui->widget->xAxis->setTickLabelRotation(60);

    ui->widget->yAxis->setRange(0,5000000);
    ui->widget->yAxis->setNumberPrecision(1000000);

    ui->widget->legend->setVisible(true);
    ui->widget->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);

    ui->widget->graph(0)->setName("累计确诊");
    ui->widget->graph(1)->setName("现有确诊");
    ui->widget->graph(0)->setData(data->dateArr,data->totalConfirmArr);
    ui->widget->graph(1)->setData(data->dateArr,data->storeConfirmArr);
    ui->widget->graph(0)->setPen(QPen(QColor(0,0,255)));
    ui->widget->graph(1)->setPen(QPen(QColor(255,0,0)));
    ui->widget->graph(0)->setVisible(true);
    ui->widget->graph(1)->setVisible(true);
    ui->widget->graph(1)->addToLegend(ui->widget->legend);
    ui->widget->replot();
}

void Render::increaseTrend()
{
    if(!increaseTrendFlag)
    {
        increaseTrendFlag = 1;
        confirmTrendFlag = 0;
        deadAndHealTrendFlag = 0;
        ui->widget->setLocale(QLocale(QLocale::Chinese, QLocale::China));
        QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
        for (int i = 0; i < data->date.size(); i++)
        {
            textTicker->addTick(i, data->date[i]);
        }

        ui->widget->xAxis->setLabel("日期");
        ui->widget->yAxis->setLabel("数量");
        ui->widget->xAxis->setRange(data->dateArr[0],data->dateArr.size());
        ui->widget->xAxis->setTicker(textTicker);

        ui->widget->yAxis->setRange(0,100000);
        ui->widget->yAxis->setNumberPrecision(20000);

        ui->widget->legend->setVisible(true);
        ui->widget->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignLeft);

        ui->widget->graph(0)->setName("确诊");
        ui->widget->graph(0)->setPen(QPen(QColor(255,0,0)));
        ui->widget->graph(0)->setData(data->dateArr,data->todayConfirmArr);
        ui->widget->graph(1)->setVisible(false);
        ui->widget->graph(1)->removeFromLegend(ui->widget->legend);
        ui->widget->replot();
    }
}

void Render::confirmTrend()
{
    if(!confirmTrendFlag)
    {
        increaseTrendFlag = 0;
        confirmTrendFlag = 1;
        deadAndHealTrendFlag = 0;
        plot(data);
    }

}

void Render::deadAndHealTrend()
{
    if(!deadAndHealTrendFlag)
    {
        increaseTrendFlag = 0;
        confirmTrendFlag = 0;
        deadAndHealTrendFlag = 1;

        ui->widget->setLocale(QLocale(QLocale::Chinese, QLocale::China));
        QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
        for (int i = 0; i < data->date.size(); i++)
        {
            textTicker->addTick(i, data->date[i]);
        }

        ui->widget->xAxis->setLabel("日期");
        ui->widget->yAxis->setLabel("数量");
        ui->widget->xAxis->setRange(data->dateArr[0],data->dateArr.size());
        ui->widget->xAxis->setTicker(textTicker);
        ui->widget->xAxis->setTickLabelRotation(60);

        ui->widget->yAxis->setRange(0,400000);
        ui->widget->yAxis->setNumberPrecision(50000);

        ui->widget->legend->setVisible(true);
        ui->widget->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignLeft);

        ui->widget->graph(0)->setName("累计治愈");
        ui->widget->graph(1)->setName("累计死亡");
        ui->widget->graph(0)->setData(data->dateArr,data->totalHealArr);
        ui->widget->graph(1)->setData(data->dateArr,data->totalDeadArr);
        ui->widget->graph(0)->setPen(QPen(QColor(0,200,0)));
        ui->widget->graph(1)->setPen(QPen(QColor(128,138,135)));
        ui->widget->graph(0)->setVisible(true);
        ui->widget->graph(1)->setVisible(true);
        ui->widget->graph(1)->addToLegend(ui->widget->legend);
        ui->widget->replot();
    }
}
